﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;


namespace AgileDesign.Utilities
{
    public class EnvironmentInfo
    {
        object ConfigurationObject { get; set; }

        public EnvironmentInfo(object configurationObject)
        {
            ConfigurationObject = configurationObject;
        }

        public IEnumerable<ConfigPropertyTest> TestedConfigurationProperties
        {
            get
            {
                return ConfigurationObject
                    .Properties()
                    .Select(p => new ConfigPropertyTest(p));
            }
        }

        public string ApplicationVersion
        {
            get
            {
                return Assembly.GetExecutingAssembly()
                                .GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false)
                                .Cast<AssemblyFileVersionAttribute>()
                                .Single()
                                .Version;
            }
        }

        public string ControllerUser { get; set; }

        private IDictionary<string, object> currentUserProperties;
        public IDictionary<string, object> CurrentUserProperties
        {
            get
            {
                return currentUserProperties 
                    ?? (currentUserProperties = CreateUserPropertiesDictionary());
            }
        }

        private IDictionary<string, object> CreateUserPropertiesDictionary()
        {
            var result = new Dictionary<string, object>();
            result["WindowsIdentity.GetCurrent().Name"]
                = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

            result["Controller.User.Identity.Name"] = ControllerUser;
            result["Environment.UserName"] = Environment.UserName;
            result["Environment.UserDomainName"] = Environment.UserDomainName;
            return result;
        }
    }


}